﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SPA_Prototype.Model
{
    public enum SEARCH_METHOD { ALL, BILL_NO, CUSTOMER_NAME, CASHIER, ROOM };
    public enum TIME_CONSTRAIN { ALL, TODAY, YESTERDAY, LAST7DAY, THISMONTH, PERIOD };
    public class History
    {
        public DateTime MaxDate;
        public DateTime MinDate;

        private DatabaseDataSet _Dataset;

        private DatabaseDataSet.BillMainTableDataTable _MainDataTable;
        private DatabaseDataSet.BillDetailsTableDataTable _DetailsDataTable;

        private DatabaseDataSet.BillMainTableDataTable _SearchResultMainTable;
        private DatabaseDataSet.BillDetailsTableDataTable _SearchResultDetailsTable;

        private DatabaseDataSetTableAdapters.BillMainTableTableAdapter _AdtMain; 
        private DatabaseDataSetTableAdapters.BillDetailsTableTableAdapter _AdtDetails; 
                                    
        // Constructor
        public History()
        {
            _Dataset = new DatabaseDataSet();
            
            _MainDataTable = _Dataset.BillMainTable;
            _DetailsDataTable = _Dataset.BillDetailsTable;

            _AdtMain = new DatabaseDataSetTableAdapters.BillMainTableTableAdapter();
            _AdtDetails = new DatabaseDataSetTableAdapters.BillDetailsTableTableAdapter();

            _SearchResultMainTable = new DatabaseDataSet.BillMainTableDataTable();
            _SearchResultDetailsTable = new DatabaseDataSet.BillDetailsTableDataTable();

            LoadDB();
        }

        // property / field
        public int BillMainDataTableRowsCount
        {
            get { return _MainDataTable.Rows.Count; }                       
        }
        public int BillDetailsDataTableRowsCount
        {
            get { return _DetailsDataTable.Rows.Count; }
        }
        public int ToTalVND
        {
            get
            {
                int sum = 0;
                int i = _SearchResultMainTable.Rows.Count;
                for (int j = 0; j< i; j++)
                {
                       sum += _SearchResultMainTable[i].Total;
                }
                return sum;
            }
        }
        // medthod : update and load
        public void LoadDB()
        {
            _AdtMain.Fill(_MainDataTable);
            _AdtDetails.Fill(_DetailsDataTable);
        }        

        public string BillNumber(int index)
        {
            return _MainDataTable[index].Number.ToString();
        }
        public DatabaseDataSet.BillMainTableDataTable All()
        {            
            return _MainDataTable;
        }

        // Search Engine

        // string method        : "All"/ "Bill No."/ "Customer Name" / "Casher" / "Room"
        // string TimeConstrain : "Today" / "Yesterday" / "Last7Day" /  "ThisMonth"
        public DatabaseDataSet.BillMainTableDataTable Search(SEARCH_METHOD Method, TIME_CONSTRAIN TimeConstrain, string Keyword)
        {
            DateTime Today = GetDate("Today");      // hh:mm:ss = 00:00:00
            DateTime Now = DateTime.Now;
            DateTime Yesterday = GetDate("Yesterday"); // hh:mm:ss = 00:00:00 
            DateTime Last7Day = GetDate("Last7Day");   // hh:mm:ss = 00:00:00
            DateTime FirstDay = GetDate("FistDay");    // hh:mm:ss = 00:00:00
            DateTime DayZero = new DateTime(2012, 1, 1); // use as the earliest day 
            string.Format("{0:M/d/yyyy}", Today);
            string.Format("{0:M/d/yyyy}", Yesterday);
            string.Format("{0:M/d/yyyy}", Last7Day);
            string.Format("{0:M/d/yyyy}", FirstDay);            
            string.Format("{0:M/d/yyyy}", DayZero);
            string.Format("{0:M/d/yyyy}", MaxDate);
            string.Format("{0:M/d/yyyy}", MinDate);

            _SearchResultMainTable.Clear();
            if (Method == SEARCH_METHOD.ALL)
            {
                if (TimeConstrain == TIME_CONSTRAIN.TODAY) _AdtMain.FillByPeriod(_SearchResultMainTable, Today, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.YESTERDAY) _AdtMain.FillByPeriod(_SearchResultMainTable, Yesterday, Today.AddSeconds(-1));
                else if (TimeConstrain == TIME_CONSTRAIN.LAST7DAY) _AdtMain.FillByPeriod(_SearchResultMainTable, Last7Day, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.THISMONTH) _AdtMain.FillByPeriod(_SearchResultMainTable, FirstDay, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.PERIOD) _AdtMain.FillByPeriod(_SearchResultMainTable, MinDate, MaxDate);
                else _AdtMain.Fill(_SearchResultMainTable);
            }
            else if (Method == SEARCH_METHOD.BILL_NO)
            {
                _AdtMain.FillByBillNo(_SearchResultMainTable, Keyword);
            }
            else if (Method == SEARCH_METHOD.CUSTOMER_NAME)
            {
                if (TimeConstrain == TIME_CONSTRAIN.TODAY) _AdtMain.FillByCustomerName(_SearchResultMainTable, Keyword, Today, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.YESTERDAY) _AdtMain.FillByCustomerName(_SearchResultMainTable, Keyword, Yesterday, Today.AddSeconds(-1));
                else if (TimeConstrain == TIME_CONSTRAIN.LAST7DAY) _AdtMain.FillByCustomerName(_SearchResultMainTable, Keyword, Last7Day, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.THISMONTH) _AdtMain.FillByCustomerName(_SearchResultMainTable, Keyword, FirstDay, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.PERIOD) _AdtMain.FillByCustomerName(_SearchResultMainTable, Keyword, MinDate, MaxDate);
                else _AdtMain.FillByCustomerName(_SearchResultMainTable, Keyword, DayZero, Now);
            }
            else if (Method == SEARCH_METHOD.CASHIER)
            {
                if (TimeConstrain == TIME_CONSTRAIN.TODAY) _AdtMain.FillByCashier(_SearchResultMainTable, Keyword, Today, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.YESTERDAY) _AdtMain.FillByCashier(_SearchResultMainTable, Keyword, Yesterday, Today.AddSeconds(-1));
                else if (TimeConstrain == TIME_CONSTRAIN.LAST7DAY) _AdtMain.FillByCashier(_SearchResultMainTable, Keyword, Last7Day, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.THISMONTH) _AdtMain.FillByCashier(_SearchResultMainTable, Keyword, FirstDay, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.PERIOD) _AdtMain.FillByCashier(_SearchResultMainTable, Keyword, MinDate, MaxDate);
                else _AdtMain.FillByCashier(_SearchResultMainTable, Keyword, DayZero, Now);
            }
            else if (Method == SEARCH_METHOD.ROOM)
            {
                if (TimeConstrain == TIME_CONSTRAIN.TODAY) _AdtMain.FillByRoom(_SearchResultMainTable, Keyword, Today, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.YESTERDAY) _AdtMain.FillByRoom(_SearchResultMainTable, Keyword, Yesterday, Today.AddSeconds(-1));
                else if (TimeConstrain == TIME_CONSTRAIN.LAST7DAY) _AdtMain.FillByRoom(_SearchResultMainTable, Keyword, Last7Day, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.THISMONTH) _AdtMain.FillByRoom(_SearchResultMainTable, Keyword, FirstDay, Now);
                else if (TimeConstrain == TIME_CONSTRAIN.PERIOD) _AdtMain.FillByRoom(_SearchResultMainTable, Keyword, MinDate, MaxDate);
                else _AdtMain.FillByRoom(_SearchResultMainTable, Keyword, DayZero, Now);
            }
            else  MessageBox.Show("No results found", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) ;
            return _SearchResultMainTable;
        }

        public DatabaseDataSet.BillDetailsTableDataTable BillDetails(string BillNo)
        {
            _AdtDetails.FillByBillNo(_SearchResultDetailsTable, BillNo);
            return _SearchResultDetailsTable;
        }     

        // private functions
        private DateTime GetDate(string Day)
        {
            DateTime Today = DateTime.Now;
            DateTime dt = new DateTime(Today.Year, Today.Month, Today.Day, 0, 0, 0);
            int year = dt.Year;
            int month = dt.Month;
            switch (Day)
            {
                case "Today":
                    break;
                case "Yesterday":
                    dt = dt.AddDays(-1);
                    break;
                case "Last7Day":
                    dt = dt.AddDays(-6);
                    break;
                case "FistDay":
                    dt = new DateTime(year, month, 1);
                    break;
                default:
                    break;
            }
            string.Format("{0:M/d/yyyy}", dt);
            return dt;
        }
    }
}
